home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / PASSWORD.C < prev    next >
C/C++ Source or Header  |  1989-03-04  |  2KB  |  74 lines

  1. /* Verify user's identity using a password like that in NET/ROM */
  2. /* Additional protection built in using an algorithm by PE1HZG */
  3.  
  4. #include "global.h"
  5. #include "password.h"
  6.  
  7. #ifdef MSC
  8. #include <time.h>
  9. #endif
  10.  
  11. char *
  12. gen_challenge (pw)
  13. register struct password *pw;
  14. {
  15.     int row,cnt,num,chk;
  16.     int pswlen;
  17.     int retries;
  18.     static char result[P_ROWS * P_NUMS * 4 + 3];
  19.     static int initialize = 1;
  20.  
  21.     if (initialize){
  22.     srand((int) time(NULL));    /* seed the random generator */
  23.     initialize = 0;
  24.     }
  25.  
  26.     if ((pswlen = strlen(pw->paswrd)) > 999)
  27.     pswlen = 999;
  28.  
  29.     pw->okay = 0;            /* password not okay for now */
  30.     result[0] = '\0';            /* clear result */
  31.  
  32.     for (row = 0; row < P_ROWS; row++) {
  33.     for (cnt = 0; cnt < P_NUMS; cnt++) {
  34.         retries = 100;
  35.         do {
  36.         do
  37.             num = rand() % pswlen; /* 0 .. len-1 */
  38.         while (pw->paswrd[num] == ' '); /* ignore when space in passwd */
  39.  
  40.         for (chk = 0; chk < cnt; chk++) { /* no duplicates please */
  41.             if (pw->chall[row][chk] == num)
  42.             break;
  43.         }
  44.         } while (cnt != chk && retries--);
  45.  
  46.         pw->chall[row][cnt] = num;
  47.         sprintf(result + strlen(result)," %d",pw->chall[row][cnt] + 1);
  48.     }
  49.  
  50.     strcat(result,",");
  51.     }
  52.  
  53.     result[strlen(result) - 1] = '\0';    /* chop off last comma */
  54.     return (result + 1);        /* skip first space */
  55. }
  56.  
  57. int
  58. ok_password (pw,input)
  59. register struct password *pw;
  60. char *input;
  61. {
  62.     int row,cnt;
  63.  
  64.     for (row = 0; row < P_ROWS; row++) {
  65.     for (cnt = 0; cnt < P_NUMS; cnt++) {
  66.         if (pw->paswrd[pw->chall[row][cnt]] != input[cnt])
  67.         break;
  68.     }
  69.     if (cnt == P_NUMS)
  70.         return (pw->okay = 1);    /* it is correct! */
  71.     }
  72.     return 0;                /* sorry, no match */
  73. }
  74.